#!/usr/bin/perl
unshift (@INC, "/home/gslab/Subroutines");
require ("get_op_line_opchain.pl");
require ("conclusion_op_chain.pl");
sub basic_input_for_opchain{
	my($r1,$h1) = @_;                                                    #Parameters passed are config. rule and ping command. 
	`$r1`;
	$setupexec = $?;                                                     #Execution status for setup, ping and cleardown are captured in similar fashion.
	`$h1 > tst1op.txt 2>&1`;                                             #output is stored in a file.
	$ruleexec = $?;
	$aop = open(FILE1, "tst1op.txt");           
	@complete_output = <FILE1>;                                          #complete_output has the complete output of the hping3 command.
	($reqd_op_line,$flagerr,@reqd_line_split) = &get_op_line_opchain(@complete_output);           #Subroutine for getting proper line from whole output.
        	                                                             #reqd_op_line has the required output line.
        	                                                             #reqd_line_split has the required line split into components and stored in an array.
	&disp_command($rule,$hping);
	print("------------Filter table after execution of first command-----------\n");
	&disp_table;
	print("------------Output Conclusion-----------\n");
	print $reqd_op_line;                                                 #reqd_op_line has the required output line.
	($sxs_or_fail,$cmd_not_found) = &conclusion_op_chain(@reqd_line_split);
	print("----------------------------------------\n");
	system("rm tst1op.txt");
	print("-------------Table after erasing rules--------------\n");
	system("sudo iptables -F");
	&disp_table;
	$clearexec = $?;
	system("sudo iptables -P OUTPUT ACCEPT");
	$flagsetup = 0;
	$flagrule = 0;
	$flagclear = 0;
	print("-------------Results-------------\n");
	if ($setupexec == 0){                                                #for output chain module, all three parameters are checked because it occurs on the same
		print("Setup phase execution: PASS\n");                      #host.
		$flagsetup = 1;
	}
	else{
		print("Setup phase execution: FAIL\n");
		$flagsetup = 2;
	}
	if ($ruleexec == 0){
		print("Rule execution: PASS\n");
		$flagrule = 1;
	}
	else{
		if ($flagerr == 1){
			print("Rule execution: PASS\n");
			$flagrule = 1;
		}
		else{
			print("Rule execution: FAIL\n");
			$flagrule = 2;
		}
	}
	if ($clearexec == 0){
		print("Cleardown execution: PASS\n");
		$flagclear = 1;
	}
	else{
		print("Cleardown execution: FAIL\n");
		$flagclear = 2;
	}                                                                   #Flags for setup, execution and cleardown are returned along with total success or fail.
	return($flagsetup,$flagrule,$flagclear,$sxs_or_fail,$cmd_not_found,$flagerr);
}
1;

